#!/bin/sh
# postinst script for minidlna

set -e

unset USER

DEFAULT=/etc/default/minidlna

# Get values from ${DEFAULT}, if it exists.
[ -r ${DEFAULT} ] && . ${DEFAULT}

# Set the default user and user.
if [ -z ${USER} ]; then
    USER=minidlna
fi
if [ -z ${GROUP} ]; then
    GROUP=minidlna
fi

CONFIG=/etc/minidlna.conf

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

# Based on the mediatomb debian package.

case "$1" in
    configure)
        dpkg-statoverride --force --update \
            --add ${USER} ${GROUP} 644 ${CONFIG} >/dev/null 2>&1

        if [ -d /var/lib/minidlna ]; then
            dpkg-statoverride --force --update \
                --add ${USER} ${GROUP} 755 /var/lib/minidlna >/dev/null 2>&1
        fi

		# Check for XML config
		if ! omv_config_exists "//services/minidlna"; then
			omv_config_add_element "//services" "minidlna" ""
		fi
	
        if ! omv_config_exists "//services/minidlna/enable"; then
            omv_config_add_element "//services/minidlna" "enable" "0"
        fi

        if ! omv_config_exists "//services/minidlna/name"; then
            omv_config_add_element "//services/minidlna" "name" "MiniDLNA Server on OpenMediaVault"
        fi

        if ! omv_config_exists "//services/minidlna/port"; then
            omv_config_add_element "//services/minidlna" "port" "8200"
        fi

        if ! omv_config_exists "//services/minidlna/strict"; then
            omv_config_add_element "//services/minidlna" "strict" "0"
        fi

        if ! omv_config_exists "//services/minidlna/tivo"; then
            omv_config_add_element "//services/minidlna" "tivo" "0"
        fi

        if ! omv_config_exists "//services/minidlna/folders"; then
            omv_config_add_element "//services/minidlna" "folders" ""
        fi

		# Add user OMV
		if ! omv_config_exists "//system/usermanagement/users/user[name='minidlna']"; then
			omv_config_import_user "minidlna"
			omv_config_update "//system/usermanagement/users/user[name='minidlna']/comment" "MiniDLNA User"
		fi
	
		# Fix permissions
		omv_install_fixperms

    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

#DEBHELPER#

exit 0
